/*任务线程池*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <zconf.h>
#include "TThreadPool.h"


//线程池创建
void TThreadPool_create(TThreadPool *pool) {
    memset(pool, 0, sizeof(TThreadPool));
    pthread_mutex_init(&pool->thread_operate_lock, NULL);//初始化互斥锁
    pthread_mutex_init(&pool->task_operate_lock, NULL);//初始化互斥锁
}

//线程池销毁
void TThreadPool_destroy(TThreadPool *pool) {
    TThreadThread *tthread;

    //释放线程
    while (pool->thread_list != 0) {
        tthread = pool->thread_list->next;
        TThreadPool_thread_sub(pool, pool->thread_list);
        pool->thread_list = tthread;
    }


    pthread_mutex_destroy(&pool->task_operate_lock);               //销毁互斥锁
    pthread_mutex_destroy(&pool->thread_operate_lock);                  //销毁互斥锁
}


static void TThreadPool_task_add_task(TThreadPool *pool, TThreadTask *new_task) {
    if (pool->task_list == 0) {
        pool->task_list = new_task;
        new_task->last_task = new_task;
        new_task->next_task = new_task;
    } else {
        pool->task_list->last_task->next_task = new_task;
        new_task->last_task = pool->task_list->last_task;
        new_task->next_task = pool->task_list;
        pool->task_list->last_task = new_task;
    }
    pool->wait_task_number++;
    pool->task_number++;
}

//删除任务
static void TThreadPool_task_delete_task(TThreadPool *pool, TThreadTask *task) {
    if (task->next_task == task) {
        pool->task_list = 0;
    } else {
        task->last_task->next_task = task->next_task;
        task->next_task->last_task = task->last_task;
        if (task == pool->task_list)
            pool->task_list = task->next_task;
    }
}


//线程函数
static void *TThreadPool_thread_exe(TThreadThread *tthread) {

    int ret;
    TThreadPool *pool;
    pool = tthread->pool;
    TThreadTask *task = 0;
    //线程初始化等待
    pthread_mutex_lock(&tthread->pool->thread_operate_lock);
    pthread_mutex_unlock(&tthread->pool->thread_operate_lock);

    //线程不是销毁状态,一直运行
    while (tthread->thread_state != TTHREADTHREAD_STATE_WAIT_DESTROY) {

        /*从任务列表获取任务*/
        //尝试获取任务操作锁
        ret = pthread_mutex_trylock(&pool->task_operate_lock);
        if (ret == 0) {//获取到任务操作锁
            if (pool->task_list != 0) {//任务列表非空

                //判断任务状态,执行相应操作
                switch (pool->task_list->task_state) {
                    case TTHREADTASK_STATE_WAIT://等待状态
                        pool->task_list->task_state = TTHREADTASK_STATE_RUN;    //转为运行状态
                        pool->leisure_thread_number--;                          //标志空闲线程数
                        pool->wait_task_number--;                               //等待任务数
                        task = pool->task_list;
                        break;
                    case TTHREADTASK_STATE_WAIT_CANCEL://等待取消状态
                        pool->task_list->task_state = TTHREADTASK_STATE_CANCEL;    //转为取消状态
                        task = 0;
                        break;
                    default:
                        task = 0;
                        break;
                }
                pool->task_list = pool->task_list->next_task;
            }
            //释放任务操作锁
            pthread_mutex_unlock(&pool->task_operate_lock);

            //执行获取到的任务
            if (task != 0) {
                ret = task->fun(task->data); //执行任务

                switch (ret) {
                    case TTHREADTASK_RET_CANCEL: //取消任务
                        pthread_mutex_lock(&pool->task_operate_lock);
                        switch (task->task_state) {                             //判断任务状态
                            case TTHREADTASK_STATE_RUN:                         //运行状态,返回是取消任务，其没有外部取消，取消
                                task->task_state = TTHREADTASK_STATE_WAIT;       //转为等待状态
                                pool->wait_task_number++;
                                //将任务从线程池弄出
                                TThreadPool_task_delete_task(pool,task);
                                break;
                            case TTHREADTASK_STATE_WAIT_CANCEL:         //运行状态,返回是取消任务，有外部取消，有外部进行取消
                                task->task_state = TTHREADTASK_STATE_CANCEL;    //转为取消状态
                                break;
                        }
                        pool->leisure_thread_number++;                          //标志空闲线程数
                        pthread_mutex_unlock(&pool->task_operate_lock);
                        break;
                    case TTHREADTASK_RET_RUN:                                   //继续运行
                    default:
                        pthread_mutex_lock(&pool->task_operate_lock);
                        switch (task->task_state) {                             //判断任务状态
                            case TTHREADTASK_STATE_RUN:                         //运行状态,返回是运行，其没有外部取消，则运行
                                task->task_state = TTHREADTASK_STATE_WAIT;       //转为等待状态
                                pool->wait_task_number++;
                                break;
                            case TTHREADTASK_STATE_WAIT_CANCEL: //等待取消状态
                                task->task_state = TTHREADTASK_STATE_CANCEL;    //转为取消状态
                                break;
                        }
                        pool->leisure_thread_number++;                          //标志空闲线程数
                        pthread_mutex_unlock(&pool->task_operate_lock);
                        break;
                }
            }
        }
    }

    tthread->thread_state = TTHREADTHREAD_STATE_DESTROY;

    return 0;
}


//添加线程
int TThreadPool_thread_add(TThreadPool *pool, TThreadThread *new_tthread) {
    int ret;

    memset(new_tthread, 0, sizeof(TThreadThread));
    new_tthread->thread_id = 0;
    new_tthread->pool = pool;

    //加线程操作锁
    pthread_mutex_lock(&pool->thread_operate_lock);

    //创建线程
    ret = pthread_create(&(new_tthread->thread_id), NULL, (void *) TThreadPool_thread_exe, new_tthread);
    if (ret != 0) {
        pthread_mutex_unlock(&new_tthread->pool->thread_operate_lock);
        return 0;
    }

    //将线程添加到线程列表
    new_tthread->next = pool->thread_list;
    if (pool->thread_list != 0) {
        pool->thread_list->last = new_tthread;
    }
    pool->thread_list = new_tthread;
    pool->thread_number++;
    pool->leisure_thread_number++;
    //解线程操作锁
    pthread_mutex_unlock(&new_tthread->pool->thread_operate_lock);
}


//减少线程
void TThreadPool_thread_sub(TThreadPool *pool, TThreadThread *tthread) {

    tthread->thread_state = TTHREADTHREAD_STATE_WAIT_DESTROY;
    while (tthread->thread_state != TTHREADTHREAD_STATE_DESTROY);//等待线程执行完毕

    //加线程操作锁
    pthread_mutex_lock(&pool->thread_operate_lock);
    //取消线程
    pthread_cancel(tthread->thread_id);

    //将线程从线程列表去除
    if (tthread->last != 0)
        tthread->last->next = tthread->next;
    else
        pool->thread_list = tthread->next;
    if (tthread->next != 0)
        tthread->next->last = tthread->last;
    pool->thread_number--;
    pool->leisure_thread_number--;
    //解线程操作锁
    pthread_mutex_unlock(&tthread->pool->thread_operate_lock);
}

//添加任务
void TThreadPool_task_add(TThreadPool *pool, TThreadTask *new_task) {
    pthread_mutex_lock(&pool->task_operate_lock);
    TThreadPool_task_add_task(pool, new_task);
    pthread_mutex_unlock(&pool->task_operate_lock);
}

//删除任务
void TThreadPool_task_delete(TThreadPool *pool, TThreadTask *task) {

    pthread_mutex_lock(&pool->task_operate_lock);
    task->task_state = TTHREADTASK_STATE_WAIT_CANCEL;       //标记任务处于取消状态
    pthread_mutex_unlock(&pool->task_operate_lock);
    while (task->task_state != TTHREADTASK_STATE_CANCEL);   //等待任务执行完成

    pthread_mutex_lock(&pool->task_operate_lock);
    TThreadPool_task_delete_task(pool, task);
    pthread_mutex_unlock(&pool->task_operate_lock);

}

//开始任务
void TThreadPool_task_start(TThreadPool *pool, TThreadTask *task) {
    pthread_mutex_lock(&pool->task_operate_lock);
    if (task->task_state == TTHREADTASK_STATE_PAUSE) {
        task->task_state = TTHREADTASK_STATE_WAIT;
    }
    pthread_mutex_unlock(&pool->task_operate_lock);
}

//任务暂停
void TThreadPool_task_pause(TThreadPool *pool, TThreadTask *task) {
    pthread_mutex_lock(&pool->task_operate_lock);
    if (task->task_state == TTHREADTASK_STATE_WAIT) {
        task->task_state = TTHREADTASK_STATE_PAUSE;
    }
    pthread_mutex_unlock(&pool->task_operate_lock);
}

//任务设置
void TThreadPool_task_set(TThreadTask *new_task, void *fun, void *data) {
    memset(new_task, 0, sizeof(TThreadTask));
    new_task->fun = fun;
    new_task->data = data;
}





